import { createRouter, createWebHistory } from 'vue-router';
import useTokenStore from '@/stores/token'

const router = createRouter({
    history: createWebHistory(),
    routes:[
        {
            name: 'login',
            path:'/login',
            component: () => import('@/pages/login/LoginPage.vue')
        },
        {
            name: 'home',
            path: '/home',
            component: () => import('@/pages/home/HomePage.vue'),
            children:[
                {
                    name: 'knowledge',
                    path: 'knowledge',
                    component: () => import('@/pages/home/knowledge/KnowledgePage.vue'),
                },
                {
                    name: 'chat',
                    path: 'chat',
                    component: () => import('@/pages/home/chat/ChatPage.vue'),
                }
            ],
            redirect: '/home/knowledge'
        }
    ]
})

router.beforeEach((from) => {
    const tokenStore = useTokenStore()
    let isLogin = tokenStore.isLogin
    if (isLogin || from.name == 'login') {
        return true
    } else {
        return { name: 'login' }
    }
})

export default router;